import GlobalConfig from '../../config/index';
const FilePath = GlobalConfig.FilePath;
import storage from '../../cache/gh-storage';

class UploadFile {
  constructor() {

  }
  static uploadFile(opts) {
    let options = this._closetOptions(opts);
    let { url, header, filePath } = options;
    return new Promise((resolve, reject) => {
      let uploadTask = wx.downloadFile({
        url,
        header,
        filePath,
        success(res) {
          resolve(res);
        },
        fail(res) {
          reject(res)
        },
        complete() { },
      });
      uploadFile.onProgressUpdate((res) => {
        options.progress(res);
        console.log('下载进度', res.progress);
        console.log('已经下载的数据长度', res.totalBytesWritten)
        console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
      })
      // uploadFile.abort() // 取消下载任务

    });

  }
  static DownloadFile(opts) {

  }
  _closetOptions(options) {
    return {
      ...options,
      url: FilePath + options.url,
      header: {
        "AppType": GlobalConfig.AppType,
        "access_token": storage.getUserInfo()['access_token'] || ''
      },

    }
  }


}
export default {
  uploadFile,
  downloadFile,
}